<?php

class addfrdmsgModel extends Model
{
	private $tb='addfrdmsg';

	public function add($uid,$data)
	{
		//判断是否重复申请验证
		$sql="select * from $this->tb where ownid=$uid and frdid=".$data['frdid']." and done=0";
		$row=$this->db->getRow($sql);


		if($row)
		{
			//如果是，则更新申请信息
			if($this->db->update($this->tb,$data,$uid))
			{
				return $this->db->affect_row();
			}
			else
			{
				return false;
			}
		}
		else
		{
			//如果不是，则插入一条新申请
			$data['ownid']=$uid;
			if($this->db->insert($this->tb,$data))
			{
				return $this->db->insert_id();
			}
			else
			{
				return false;
			}
		}
		
	}
	
	//获取单条添加信息
	public function getone($uid)
	{
		$sql="select * from $this->tb where frdid=$uid and isread=0 limit 1";
		$row=$this->db->getRow($sql);
		if($row)
		{
			$sql="update $this->tb set isread=1 where afmid=".$row['afmid']." limit 1";
			$this->db->set_query($sql);
			return $row;
		}
		else
		{
			return false;
		}
	}
}